<?php

use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class CreatePermissionTables extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function up()
    {
        $tableNames = config('permission.table_names');
        $columnNames = config('permission.column_names');



        Schema::create($tableNames['permissions'], function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('parent_id');
            $table->string('name');
            $table->string('display_name', 50)->comment('名称');
            $table->string('url', 255);
            $table->string('role_name', 255)->comment('前端路由');
            $table->integer('type')->default(1)->comment('前端路由');
            $table->string('guard_name');
            $table->smallInteger('sort')->comment('排序，数字越大越在前面');
            $table->timestamps();
        });

        Schema::create($tableNames['roles'], function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('description', 200);
            $table->string('guard_name');
            $table->integer('is_disable')->default(2);
            $table->timestamps();
        });

        Schema::create($tableNames['logs'], function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('admin_id');
            $table->string('user_name');
            $table->string('description', 255);
            $table->string('guard_name');
            $table->string('method');
            $table->json('params');
            $table->string('ip', 55);
            $table->timestamps();
        });


        Hyperf\Utils\ApplicationContext::getContainer()->get(Psr\SimpleCache\CacheInterface::class)->delete(config('permission.cache.key'));
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        $tableNames = config('permission.table_names');

        Schema::drop($tableNames['role_has_permissions']);
        Schema::drop($tableNames['model_has_roles']);
        Schema::drop($tableNames['model_has_permissions']);
        Schema::drop($tableNames['roles']);
        Schema::drop($tableNames['permissions']);
    }
}
